﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Web.Routing;
using System.Web.Mvc;
using Ninject;
using Ninject.Modules;
using MvcCarConsume.Domain;


namespace MvcCarConsume.WebUI
{
    /// <summary>
    /// the ProfileControllerFactory override the GetControllerInstance and support controller constructing with parameters
    /// </summary>
    public class ProfileControllerFactory : DefaultControllerFactory
    {
        private class FacotryBindingTable : NinjectModule
        {
            public override void Load()
            {
                //Bind<IProductsRepository>()
                //    .To<SqlProductsRepository>()
                //    .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString
                //    );
                Bind<ICarService>()
                    .To<SqlCarService>()
                    .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MvcCarDBEntities"].ConnectionString);
                Bind<IAccountService>()
                    .To<SqlAccountService>();
                Bind<ICartService>()
                    .To<SqlCartService>()
                     .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MvcCarDBEntities"].ConnectionString);
                Bind<IAdminService>()
                    .To<SqlAdminService>()
                     .WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MvcCarDBEntities"].ConnectionString);
            }
        }

        private IKernel kernel = new StandardKernel(new FacotryBindingTable());

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
                return null;
            return (IController)kernel.Get(controllerType);
        }

    }
}